package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import com.ty.basic.handler.exceptionhandler.exception.TyException;
import cqrtplm.service.RTChangeNoticeService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@CustomListener(category = "润通", description = "检查变更对象是否已生效")
public class RTCheckChangeStatusListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(RTCheckChangeStatusListener.class);
    @CustomListenerField(
            description = "类型内部名称(等价于逻辑标识符)",
            defaultValue = "",
            required = true
    )
    private FixedValue targetStatusKey;

    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info(
                "----------润通----------开始检查变更对象是否已生效------workFlowContainerId:{}----->>>",
                workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        if (this.targetStatusKey == null) {
            log.info("检查变更对象状态，监听参数targetStatusKey为空，请给对应监听参数设置值！");
        } else {
            RTChangeNoticeService rtChangeNoticeService = SpringUtil.getBean(RTChangeNoticeService.class);
            rtChangeNoticeService.checkChangeStatus(workFlowContainerId, this.targetStatusKey.getExpressionText());
        }
        log.info("----------检查变更对象是否已生效：{}---->>>", System.currentTimeMillis() - start);
    }


}
